package com.fg.cloud.framework.file;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.ServiceException;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.auth.QiniuInfo;
import com.fg.cloud.common.auth.QiniuUtils;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.framework.controller.BaseController;

/**
 * Description:全局图片上传接口，配合qiniu使用
 * @author around
 * @date 2017年8月10日下午2:36:23
 */
@Controller
@RequestMapping("/qiniu")
public class QiniuController extends BaseController {
	
	/**
	 * Description:其他基本业务图片上传
	 * @param response
	 * @param key
	 * @author around
	 * @date 2017年8月14日下午1:47:02
	 */
	@RequestMapping(value = "/baseToken", method = {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public void getToken(HttpServletResponse response, String key) {
		try {
			doResult(response, true, StatusCode.FINDSUCCESS,
					new QiniuInfo(key, QiniuUtils.CLOUD_URL, 
							QiniuUtils.getCustomToken(QiniuUtils.CLOUD_BUCKET, key)),
					ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(response, false, StatusCode.FINDERROR, null, "token返回失败");
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:商品图片上传
	 * @param response
	 * @param key
	 * @author around
	 * @date 2017年8月14日下午1:45:26
	 */
	@RequestMapping(value = "/productToken", method = {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public void getCustomToken(HttpServletResponse response, String key) {
		try {
			doResult(response, true, StatusCode.FINDSUCCESS, 
					new QiniuInfo(key, QiniuUtils.PRODUCT_URL, 
							QiniuUtils.getCustomToken(QiniuUtils.PRODUCT_BUCKET, key)),
					ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(response, false, StatusCode.FINDERROR, null, "token返回失败");
			e.printStackTrace();
		}
	}
	
	
	/**
	 * Description:单文件图片删除
	 * @param response
	 * @param url
	 * @author around
	 * @date 2017年8月14日下午1:44:31
	 */
	@RequestMapping(value = "/delete", method = {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public void delete(HttpServletResponse response, String url) {
		if(StringUtils.isEmpty(url)) {
			doResult(response, true, StatusCode.REMOVEERROR, null, "图片地址为空");
			return;
		}
		try {
			String bucket = checkUrl(url);
			String key = url.substring(url.lastIndexOf("/")+1);
			boolean flag = QiniuUtils.select(bucket, key);
			if (!flag) {
				doResult(response, false, StatusCode.REMOVEERROR, null, "未找到图片或已经删除");
				return;
			}
			flag = QiniuUtils.remove(bucket, key);
			doResult(response, flag, flag ? StatusCode.DEFAULT:StatusCode.REMOVEERROR, 
					null, flag ? ResultType.REMOVE_SUCCESS:ResultType.REMOVE_FAULT);
		} catch (ServiceException e) {
			doResult(response, false, e.getCode(), null, "找不到图片或图片已经删除");
			e.printStackTrace();
		} catch (Exception e) {
			doResult(response, false, StatusCode.REMOVEERROR, null, ResultType.REMOVE_FAULT);
			e.printStackTrace();
		}
	}
	
	
	/**
	 * Description:批量删除图片
	 * @param response
	 * @param urls 传递URLs
	 * @author around
	 * @date 2017年8月14日下午1:56:20
	 */
	@RequestMapping(value = "/deletes", method = {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public void deletes(HttpServletResponse response, String urls) {
		if(StringUtils.isEmpty(urls)) {
			doResult(response, true, StatusCode.REMOVEERROR, null, "图片地址为空");
			return;
		}
		try {
			String[] urlList = urls.split("[, ]+");
			String[] keyList = new String[urlList.length];
			
			for (int i = 0; i < urlList.length; i++) {
				keyList[i] = urlList[i].substring(urlList[i].lastIndexOf("/")+1);
			}
			boolean flag = QiniuUtils.remove(checkUrl(urlList[0]), keyList);
			doResult(response, flag, flag ? StatusCode.DEFAULT:StatusCode.REMOVEERROR, 
					null, flag ? ResultType.REMOVE_SUCCESS:ResultType.REMOVE_FAULT);
		} catch (ServiceException e) {
			doResult(response, false, e.getCode(), null, "找不到图片或图片已经删除");
			e.printStackTrace();
		} catch (Exception e) {
			doResult(response, false, StatusCode.REMOVEERROR, null, ResultType.REMOVE_FAULT);
			e.printStackTrace();
		}
	}
	
	
	/**
	 * Description:检查URL，判别是哪个图片空间
	 * @param url
	 * @return bucket
	 * @author around
	 * @date 2017年8月14日下午1:49:40
	 */
	private String checkUrl(String url) {
		//比对是哪个URL路径的图片，不同路径对应不同不同存储空间
		if (url.indexOf(QiniuUtils.CLOUD_URL) != -1) return QiniuUtils.CLOUD_BUCKET;
		else if (url.indexOf(QiniuUtils.PRODUCT_URL) != -1) return QiniuUtils.PRODUCT_BUCKET;
		else return QiniuUtils.CLOUD_BUCKET;
	}
	
	public static void main(String[] args) {
		String str = "http://192.168.1.199:8080/8db58bf7-c8c2-4bfd-b352-ab61b94554dd";
		System.out.println(str.substring(0, str.lastIndexOf("/")));
	}
	
}
